package com.qst.controller;

import com.qst.domain.Record;
import com.qst.domain.User;
import com.qst.entity.PageResult;
import com.qst.service.RecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;

@RequestMapping("/record")
@Controller
public class RecordController {
    @Autowired
    private RecordService recordService;
    /**
     * 查询借阅记录
     */
    @RequestMapping("/searchRecords")
    private ModelAndView searchRecords(Record record, HttpServletRequest request, Integer pageNum, Integer pageSize){
        if (pageNum == null){
            pageNum = 1;
        }
        if (pageSize == null){
            pageSize = 10;
        }
        // 获取当前用户的登录信息
        User user = (User) request.getSession().getAttribute("USER_SESSION");
        PageResult pageResult = recordService.searchRecords(record, user, pageNum, pageSize);
        ModelAndView mv = new ModelAndView();
        // 设置视图
        mv.setViewName("record");
        // 将查询到的数据存在ModelAndView中
        mv.addObject("pageResult",pageResult);
        // 将查询的参数返回到页面中,用于回显到查询的输入框中
        mv.addObject("search",record);
        // 将当前页码返回到页面中,用于分页插件的显示
        mv.addObject("pageNum",pageNum);
        // 将当前查询的控制器路径返回到页面,页码变化时,继续向该路径发送请求
        mv.addObject("gourl",request.getRequestURI());
        return mv;
    }
}
